﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoVanHocTapData.Interface;
using CoVanHocTapData.Entities;

namespace CoVanHocTapData.Concretes
{
    public class ChuanDauRaRepository:IChuanDauRaRepository
    {
        DangKyPhuEntities entities;

        /// <summary>
        /// Thuoc tinh Loai: 0 - tin chi tl
        /// 1 - dtbtl
        /// </summary>

        #region Constructor
        public ChuanDauRaRepository()
        {
            entities = new DangKyPhuEntities();
        }
        #endregion

        public float ChuanToeicCua(string maSV)
        {
            NganhDaoTao nganh = new NganhDaoTaoRepository().LayNganhTuMaSV(maSV);
            HeDaoTao heDaoTao = new HeDaoTaoRepository().LayHeDaoTaoTuMaSV(maSV);
            NienKhoa nienKhoa = new NienKhoaRepository().LayNienKhoaTuMaSV(maSV);
            string chuanToeic = null;
            int nienKhoaID = nienKhoa.NienKhoaID;
            while (true)
            {
                if (chuanToeic == null || chuanToeic == "")
                {
                    chuanToeic = (from m in entities.ChuanDauRas
                                  where m.NganhID == nganh.NganhID
                                  && m.HeDaoTaoID == heDaoTao.HeDaoTaoID
                                  && m.NienKhoaID == nienKhoaID
                                  && m.MoTa.ToLower().Contains("toeic")
                                  select m.GiaTri).FirstOrDefault();
                    nienKhoaID--;
                }
                else
                    break;
            }
            
            float kq = int.Parse(chuanToeic);
            return kq;
        }




        public float SoHocPhanGDQPDaHTCua(string maSV)
        {
            NganhDaoTao nganh = new NganhDaoTaoRepository().LayNganhTuMaSV(maSV);
            HeDaoTao heDaoTao = new HeDaoTaoRepository().LayHeDaoTaoTuMaSV(maSV);
            NienKhoa nienKhoa = new NienKhoaRepository().LayNienKhoaTuMaSV(maSV);

            var count = (from m in entities.ChuanDauRas
                         where m.NganhID == nganh.NganhID
                              && m.HeDaoTaoID == heDaoTao.HeDaoTaoID
                              && m.NienKhoaID == nienKhoa.NienKhoaID
                              && m.MoTa.Contains("GDQP")
                         select m).Count();
            return count;
        }

        public float SoHocPhanGDTCDaHTCua(string maSV)
        {
            NganhDaoTao nganh = new NganhDaoTaoRepository().LayNganhTuMaSV(maSV);
            HeDaoTao heDaoTao = new HeDaoTaoRepository().LayHeDaoTaoTuMaSV(maSV);
            NienKhoa nienKhoa = new NienKhoaRepository().LayNienKhoaTuMaSV(maSV);

            var count = (from m in entities.ChuanDauRas
                         where m.NganhID == nganh.NganhID
                              && m.HeDaoTaoID == heDaoTao.HeDaoTaoID
                              && m.NienKhoaID == nienKhoa.NienKhoaID
                              && m.MoTa.Contains("GDTC")
                         select m).Count();
            return count;
        }



        #region IChuanDauRaRepository Members

        /// <summary>
        /// Lay chuan dau ra voi loai la tinchiTL(0), dtbtl(1), con lai(null)
        /// </summary>
        /// <param name="nganh"></param>
        /// <param name="nienKhoa"></param>
        /// <param name="he"></param>
        /// <returns></returns>
        public IQueryable<ChuanDauRa> LayChuanDauRaTinChi(string nganhID, int nienKhoaID, string heDaoTaoID, int loai)
        {
            return from m in entities.ChuanDauRas
                   where m.NganhID.Equals(nganhID)
                   && m.NienKhoa.NienKhoaID == nienKhoaID
                   && m.HeDaoTaoID.Equals(heDaoTaoID)
                   && m.Loai.Value== loai
                   select m;

        }

        #endregion


        public void ThemChuanDauRa(ChuanDauRa item)
        {
            entities.AddToChuanDauRas(item);
            entities.SaveChanges();
        }


        public void ThemChuanDauRa(List<ChuanDauRa> ChuanDauraList)
        {
            foreach (var item in ChuanDauraList)
            {
                //if (!KiemTraChuaDauRa(item))
                    entities.AddToChuanDauRas(item);
                //else
                //    UpdateChuanDaura(item);
            }
            entities.SaveChanges();
        }


        public bool KiemTraChuaDauRa(ChuanDauRa item)
        {
            IQueryable<ChuanDauRa> result = null;
            if (item.MonHocID == null)
            {
                result = from m in entities.ChuanDauRas
                         where m.NganhID.Equals(item.NganhID)
                         && m.HeDaoTaoID.Equals(item.HeDaoTaoID)
                         && m.NienKhoaID.Value.Equals(item.NienKhoaID.Value)

                         select m;
            }
            else
            {
                result = from m in entities.ChuanDauRas
                         where m.NganhID.Equals(item.NganhID)
                         && m.HeDaoTaoID.Equals(item.HeDaoTaoID)
                         && m.NienKhoaID.Value.Equals(item.NienKhoaID.Value)
                         && m.MonHocID.Equals(item.MonHocID)
                         select m;
            }
            if (result.Count() > 0)
                return true;
            
            return false;
        }


        public void UpdateChuanDaura(ChuanDauRa item)
        {
            ChuanDauRa result = new ChuanDauRa();
            if(item.MonHocID==null)
                result = (from m in entities.ChuanDauRas
                          where m.HeDaoTaoID.Equals(item.HeDaoTaoID)
                          && m.NganhID.Equals(item.NganhID)
                          && m.NienKhoaID.Value.Equals(item.NienKhoaID.Value)
                          
                          select m).FirstOrDefault();
            else
                result = (from m in entities.ChuanDauRas
                         where m.HeDaoTaoID.Equals(item.HeDaoTaoID)
                         && m.NganhID.Equals(item.NganhID)
                         && m.NienKhoaID.Value.Equals(item.NienKhoaID.Value)
                         && m.MonHocID.Equals(item.MonHocID)
                         select m).FirstOrDefault();
            result.MoTa = item.MoTa;
            result.GiaTri = item.GiaTri;
            result.Loai = item.Loai;
            entities.SaveChanges();
        }


        public bool KiemTraChuanDauRa(string heDaoTaoID, string nganhID, int nienKhoaID)
        {
            var result = from m in entities.ChuanDauRas
                         where m.HeDaoTaoID.Equals(heDaoTaoID)
                         && m.NganhID.Equals(nganhID)
                         && m.NienKhoaID.Value == nienKhoaID
                         select m;
            if (result.Count() > 0)
                return true;
            return false;
        }


        public IQueryable<ChuanDauRa> LayChuanDauRa(string nganh, int nienKhoaID, string heDaoTaoID)
        {
            return from m in entities.ChuanDauRas
                   where m.NganhID.Equals(nganh)
                   && m.NienKhoaID.Value == nienKhoaID
                   && m.HeDaoTaoID.Equals(heDaoTaoID)
                   select m;
        }


        public IQueryable<NganhDaoTao> LayNganh()
        {
            return from m in entities.ChuanDauRas
                   join n in entities.NganhDaoTaos
                   on m.NganhID equals n.NganhID
                   select n;
        }

        public IQueryable<HeDaoTao> LayHeDaoTao()
        {
            return from m in entities.ChuanDauRas
                   join n in entities.HeDaoTaos
                   on m.HeDaoTaoID equals n.HeDaoTaoID
                   select n;
        }

        public IQueryable<NienKhoa> LayNienKhoa()
        {
            return from m in entities.ChuanDauRas
                   join n in entities.NienKhoas
                   on m.NienKhoaID equals n.NienKhoaID
                   select n;
        }
        

        public int ChuanDGRL(string maNganh, string maHeDaoTao, int maNienKhoa)
        {
            string maMHDGRL = new MonHocRepository().MonHocDGRL().MonHocID;
            string chuan = null;
            while (true)
            {
                if (chuan == null)
                {
                    chuan = (from m in entities.ChuanDauRas
                                       where m.NganhID == maNganh
                                       && m.HeDaoTaoID == maHeDaoTao
                                       && m.NienKhoaID == maNienKhoa
                                       && m.MonHocID == maMHDGRL
                                       select m.GiaTri).FirstOrDefault();
                    maNienKhoa--;
                }
                else
                    break;
            }
           
            return int.Parse(chuan);
        }




        public ChuanDauRa LayChuanDauRaTheoID(int id)
        {
            return (from m in entities.ChuanDauRas
                   where m.ChuanDauRaID == id
                   select m).FirstOrDefault();
        }




        public void XoaChuanDauRa(ChuanDauRa item)
        {
            entities.DeleteObject(item);
            entities.SaveChanges();
        }
    }
}
